from django.shortcuts import render
from rest_framework.views import APIView
from .models import *
from .ser import *
from rest_framework.response import Response
from django.core.paginator import Paginator
from django.contrib.auth.hashers import make_password, check_password


# Create your views here.

class AshopAPI(APIView):
    def get(self, request):
        cpage = request.GET.get("cpage")
        ashop = Ashop.objects.all()
        p = Paginator(ashop, 2)
        allshop = p.page(cpage)
        ser = AshopSer(allshop, many=True)
        return Response({"code": 200, "list": ser.data, "numm": p.num_pages})

    def post(self, request):
        username = request.data.get('username')
        password = request.data.get('password')
        user = Users.objects.filter(username=username).first()
        if user:
            if user.password == password:
                return Response({"code": 200, "userid": user.id})
        return Response({"code": 20010, "mes": "错误"})


class AspAPI(APIView):
    def post(self, request):
        ser = AshopSer(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({"code": 200})
        else:
            print(ser.errors)
            return Response({"code": 10010})


class TeaAPI(APIView):
    def post(self, request):
        request.data['password'] = make_password(request.data.get('password'))
        ser = TeaSer(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({"code": 200})
        else:
            return Response({"code": 10010})

    def get(self, request):
        id = request.GET.get('id')
        tea = Tea.objects.filter(id=id).first()
        ser = TeaSer(tea)
        return Response({"code": 200, "list": ser.data})


class Tdl(APIView):
    def post(self, request):
        username = request.data.get('username')
        password = request.data.get('password')
        user = Tea.objects.filter(username=username).first()
        if user:
            if check_password(password, user.password):
                return Response({"code": 200, "mes": "登录成功", "userid": user.id})
            else:
                return Response({"code": 10010, "mes": "登录失败"})
        else:
            return Response({"code": 10020, "mes": "木有该用户"})


class StusAPI(APIView):
    def post(self, request):
        ser = StuSer(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({"code": 200})
        else:
            return Response({"code": 20010})

    def get(self, request):
        stu = Stu.objects.filter(teacher_id=4)
        ser = StuSer(stu, many=True)
        return Response({"code": 200, "list": ser.data})

    def put(self, request):
        id = request.GET.get('id')
        usersid = request.GET.get('usersid')
        print("******************" + id, usersid)
        stu = Stu.objects.filter(id=id).first()
        stu.teacher_id = usersid
        print("@@@@@@@" + stu.teacher_id, usersid)
        stu.save()
        return Response({"code": 200})


class GradeAPI(APIView):
    def get(self, request):
        gra = Grade.objects.all()
        ser = GradeSer(gra, many=True)
        return Response({"code": 200, "list": ser.data})


class PdlAPI(APIView):
    def post(self, request):
        username = request.data.get("username")
        password = request.data.get("password")
        user = Friend.objects.filter(username=username).first()
        if user:
            if user.password == password:
                return Response({"code": 200, "name": user.name, "banji": user.niaji_id, "userid": user.id})
        return Response({"code": 20020})

    def get(self, request):
        id = request.GET.get("id")  # 班级id
        gra = Grade.objects.filter(id=id).first()
        ser = GradeSer(gra)
        return Response({"code": 200, "list": ser.data})

    def put(self, request):
        uid = request.GET.get("uid")
        firend = Friend.objects.filter(id=uid).first()
        if firend.niaji_id == 3:
            return Response({"code": 10010})
        else:
            firend.niaji_id += 1
            firend.save()
            return Response({"code": 200,"aaa":firend.niaji_id})

class PeoAPI(APIView):
    def get(self,request):
        peo = PeopleM.objects.all()
        ser = PeoSer(peo,many=True)
        return Response({"code":200,"list":ser.data})

    def put(self,request):
        type = request.GET.get('type')
        if type == '1':
            aa = PeopleM.objects.all().order_by("-"+'age')
            ser = PeoSer(aa, many=True)
            type = 2

        else:

            aa = PeopleM.objects.all().order_by('age')
            ser = PeoSer(aa, many=True)
            type = 1
        return Response({"code":200,"type":type,"list":ser.data})









